package cc.comac.saic.hkhy.jsrj.gj.inttools.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cc.comac.saic.hkhy.jsrj.gj.inttools.constant.FileType;
import cc.comac.saic.hkhy.jsrj.gj.inttools.core.R;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.vo.FileTypeStatVo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.service.FileInfoService;
import jakarta.servlet.http.HttpServletResponse;

/**
 * 文件信息(FileInfo)表控制层
 *
 * @author makejava
 * @since 2025-07-02 18:26:30
 */
@RestController
@RequestMapping("file")
public class FileInfoController {
    @Autowired
    private FileInfoService fileInfoService;

    @PostMapping("upload")
    public R<?> upload(@RequestParam String projKey, @RequestParam String fileType, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.getFileTypeByCode(fileType), files));
    }

    @PostMapping("download")
    public void download(HttpServletResponse response, @RequestParam String projKey, @RequestParam String fileType, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, fileType, filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName.toString() + "\"");
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 8.1.3	开发数据版本查询接口
     */
    @GetMapping("data-version")
    public R<?> getDataVersion(@RequestParam List<String> dataKeys) {
        return R.ok(dataKeys.stream().map(s -> fileInfoService.queryVersionByDataKey(s)).collect(Collectors.toList()));
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 8.1.4	高层需求模型上传接口
     */
    @PostMapping("hlr-model/upload")
    public R<?> hlrModelUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.HLR_MODEL, files));
    }

    /**
     * 8.1.5	一致性检查报告上传接口
     */
    @PostMapping("hlr-consistency/upload")
    public R<?> hlrConsistencyUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.HLR_CONSISTENCY, files));
    }

    /**
     * 8.1.7	架构模型上传接口
     */
    @PostMapping("archi-model/upload")
    public R<?> archiModelUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.ARCHI_MODEL, files));
    }

    /**
     * 8.1.8	控制软件模型上传接口
     */
    @PostMapping("control-model/upload")
    public R<?> controlModelUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.CONTROL_MODEL, files));
    }

    /**
     * 8.1.10	显示软件模型上传接口
     */
    @PostMapping("display-model/upload")
    public R<?> displayModelUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.DISPLAY_MODEL, files));
    }

    /**
     * 8.1.9	控制模型生成代码上传接口
     */
    @PostMapping("control-code/upload")
    public R<?> controlCodeUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.CONTROL_CODE, files));
    }

    /**
     * 8.1.11	显示模型生成代码上传接口
     */
    @PostMapping("display-code/upload")
    public R<?> displayCodeUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.DISPLAY_CODE, files));
    }

    /**
     * 8.1.14	工程代码上传接口
     */
    @PostMapping("impl-code/upload")
    public R<?> implCodeUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.IMPL_CODE, files));
    }

    /**
     * 8.1.16	测试案例上传接口
     */
    @PostMapping("test-case/upload")
    public R<?> testCaseUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.TEST_CASE, files));
    }

    /**
     * 8.1.17	静态分析报告上传接口
     */
    @PostMapping("static-report/upload")
    public R<?> staticReportUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.STATIC_REPORT, files));
    }

    /**
     * 8.1.18	插桩代码上传接口
     */
    @PostMapping("instr-code/upload")
    public R<?> instrCodeUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.INSTR_CODE, files));
    }

    /**
     * 8.1.19	测试报告上传接口
     */
    @PostMapping("test-report/upload")
    public R<?> testReportUpload(@RequestParam String projKey, @RequestParam List<MultipartFile> files) {
        return R.ok(fileInfoService.upload(projKey, FileType.TEST_REPORT, files));
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 8.1.6	高层需求模型获取接口
     */
    @PostMapping("hlr-model/download")
    public void hlrModelDownload(HttpServletResponse response,
                                 @RequestParam String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.HLR_MODEL.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName.toString() + "\"");
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 8.1.12	控制模型生成代码获取接口
     */
    @PostMapping("control-code/download")
    public void controlCodeDownload(HttpServletResponse response,
                                    @RequestParam String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.CONTROL_CODE.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName.toString() + "\"");
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 8.1.13	显示模型生成代码获取接口
     */
    @PostMapping("display-code/download")
    public void displayCodeDownload(HttpServletResponse response,
                                    @RequestParam String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.DISPLAY_CODE.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName.toString() + "\"");
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 8.1.15	工程代码获取接口
     */
    @PostMapping("impl-code/download")
    public void implCodeDownload(HttpServletResponse response,
                                 @RequestParam String projKey, @RequestParam String filePath, @RequestParam(required = false) String versionHash) {
        try (OutputStream out = response.getOutputStream()) {
            byte[] download = fileInfoService.download(projKey, FileType.IMPL_CODE.getCode(), filePath, versionHash);

            // 设置响应头
            Path fileName = Paths.get(filePath).getFileName();
            response.setContentType("text/plain;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName.toString() + "\"");
            out.write(download);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取项目文件类型统计信息
     */
    @GetMapping("project-file-stats")
    public R<List<FileTypeStatVo>> getProjectFileStats(@RequestParam("projectId") Long projectId) {
        return R.ok(fileInfoService.getFileTypeStatsByProjectId(projectId));
    }
}

